﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
namespace Exact_Extension
{
    public partial class SectionMaster : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        public SectionMaster(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadData();
        }
        private void LoadData()
        {
            var querySec = from Sec in DS.ISHCMC_Sections
                           select Sec;
            this.iSHCMCSectionBindingSource.DataSource = querySec;

            var querySchool = from School in DS.ISHCMC_V_Schools
                              select School;
            this.iSHCMCVSchoolBindingSource.DataSource = querySchool;

            var querySchoolYear = from SchoolYear in DS.ISHCMC_SchoolYears
                                  select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySchoolYear;
            
            
        }
        private bool CheckSectionExist(string section, string schoolyear,string school)
        {
            var query = from Sec in DS.ISHCMC_Sections
                        where Sec.SectionCode == section && Sec.SchoolYear == schoolyear && Sec.School == school
                        select Sec;
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void gridView_Section_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            ISHCMC_Section itemSec = (ISHCMC_Section)this.gridView_Section.GetRow(this.gridView_Section.GetSelectedRows()[0]);
            if (itemSec.DueDate.HasValue && itemSec.SchoolYear != null && itemSec.School != null
                && itemSec.StartDate.HasValue && itemSec.EndDate.HasValue)
            {
                if (this.CheckSectionExist(itemSec.SectionCode, itemSec.SchoolYear,itemSec.School))
                {
                    ISHCMC_Section itemUpdate = DS.ISHCMC_Sections.Single(c => c.SectionCode == itemSec.SectionCode
                        && c.SchoolYear == itemSec.SchoolYear && c.School.Trim() == itemSec.School.Trim());
                    itemUpdate.DueDate = itemSec.DueDate;
                    itemUpdate.SchoolYear = itemSec.SchoolYear;
                    itemUpdate.School = itemSec.School;
                    itemUpdate.Descriptions = itemSec.Descriptions;
                    itemUpdate.SectionCode = itemSec.SectionCode;
                    itemUpdate.PaymentMethod = itemSec.PaymentMethod;
                    itemUpdate.StartDate = itemSec.StartDate;
                    itemUpdate.EndDate = itemSec.EndDate;

                    DS.SubmitChanges();
                }
                else
                {
                    ISHCMC_Section itemAdd = new ISHCMC_Section
                    {
                        SectionCode = itemSec.SectionCode,
                        Descriptions = itemSec.Descriptions,
                        DueDate = itemSec.DueDate,
                        SchoolYear = itemSec.SchoolYear,
                        School = itemSec.School,
                        PaymentMethod = itemSec.PaymentMethod,
                        StartDate = itemSec.StartDate,
                        EndDate = itemSec.EndDate
                    };
                    DS.ISHCMC_Sections.InsertOnSubmit(itemAdd);
                    DS.SubmitChanges();
                }
                this.LoadData();
            }
            else
            {
                MessageBox.Show("Due Date & School Year & School haven't fill yet!!");
            }
        }

      
    }
}